package com.hsq.db137.demos.seriver.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hsq.db137.demos.entity.Rolemenu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hsq.db137.demos.entity.Userrole;
import com.hsq.db137.demos.mapper.UserroleMapper;
import com.hsq.db137.demos.seriver.UserroleService;
/**
 * @Author: hsq加油
 * @CreateTime: 2024/12/24-11:48
 * @Description: TODO
 * @Version: 1.0
 */
@Service
public class UserroleServiceImpl extends ServiceImpl<UserroleMapper, Userrole> implements UserroleService{

    @Autowired
    UserroleMapper userroleMapper;


    /**
     * 根据用户ID和角色ID查询用户角色是否重复
     * @param userrole 用户角色表
     * @return 提示语句
     */
    @Override
    public boolean selectByRoleidAndUserid(int roleid, int userid) {
        QueryWrapper<Userrole> wrapper = new QueryWrapper<>();
        wrapper.eq("roleid", roleid);
        wrapper.eq("userid",userid);
        int size = userroleMapper.selectList(wrapper).size();
        System.out.println("size = " + size);
        return size>0?false:true;
    }



    /**
     * 新镇用户和菜单表
     * @param userrole
     * @return
     */
    @Override
    public String insertUserrole(Userrole userrole) {

       return userroleMapper.insert(userrole)>0?"新增成功":"新增失败";

//        List<Integer> s=new ArrayList<Integer>();
//        List<Integer> rid=userrole.getRoleidList();
//        for (int i = 0; i < rid.size(); i++) {
//            System.out.println("userrole.getRoleidList().get(i) = " + rid.get(i));
//            userrole.setRoleid(rid.get(i));
//            userrole.setRoleidList(null);
//            int  s1 = userroleMapper.insert(userrole);
//            s.add(s1);
//        }
//        String aa="新增失败";
//        for(int msg: s){
//            if (msg==1){
//                aa="新增成功";
//            }
//        }
//        return aa;
    }

    /**
     * 根据用户ID和角色ID删除这个用户的角色
     * @param userrole 用户角色类
     * @return 提示语句
     */
    @Override
    public String delebyUseridAndRoleid(Userrole userrole) {
        QueryWrapper<Userrole> wrapper = new QueryWrapper<>();
        wrapper.eq("roleid",userrole.getRoleid());
        wrapper.eq("userid",userrole.getUserid());

        return   userroleMapper.delete(wrapper)>0?"删除成功":"删除失败";
    }
}
